package com.sskj.givecoin.p.fragment;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.givecoin.MyAppLication;
import com.sskj.givecoin.common.Constants;
import com.sskj.givecoin.m.HttpConfig;
import com.sskj.givecoin.m.bean.HttpData;
import com.sskj.givecoin.m.bean.UserBean;
import com.sskj.givecoin.m.http.JsonCallBack;
import com.sskj.givecoin.p.base.BasePresenter;
import com.sskj.givecoin.utils.MD5Util;
import com.sskj.givecoin.utils.SPUtils;
import com.sskj.givecoin.utils.ToastUtil;
import com.sskj.givecoin.v.fragment.LoginFragment;

public class LoginFragmentPresenter extends BasePresenter<LoginFragment> {
    /**
     * 登录
     *
     * @param mobile 账号
     * @param pws    密码
     */
    public void login(String mobile, String pws) {
        OkGo.<HttpData<UserBean>>post(HttpConfig.BASE_URL + HttpConfig.LOGIN)
                .params("mobile", mobile)
                .params("opwd", MD5Util.md5FiveTime(pws))
                .execute(new JsonCallBack<HttpData<UserBean>>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData<UserBean>> response) {
                        HttpData<UserBean> httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_MOBILE, mobile);
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_TOKEN, httpListData.getData().getToken());
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_ACCOUNT, httpListData.getData().getAccount());
                            SPUtils.put(MyAppLication.getApplication(), Constants.GOOGLETYPE, httpListData.getData().getIs_start_google());
                            //重新初始化公共头
                            MyAppLication.getApplication().initOkgoHttp();
                            mView.loginSuccess(httpListData.getData().getIs_start_google());
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }
                });
    }
}
